package com.cheer.controller;

import com.cheer.dto.StandingBookDocumentSaveDTO;
import com.cheer.dto.StandingBookSaveInReviewDTO;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.StandingBookReviewService;
import com.cheer.service.StandingBookService;
import com.cheer.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/book/review")
public class StandingBookReviewController {

    @Autowired
    private StandingBookReviewService standingBookReviewService;

    @Autowired
    private StandingBookService standingBookService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    /**
     * 分页查询备案审核
     */
    @GetMapping("/list")
    public Result<Page<StandingBookReviewVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                   @RequestParam(required = false) Long orgId,
                                                   @RequestParam(required = false) String applicantName,
                                                   @RequestParam(required = false) String contactPerson,
                                                   @RequestParam(required = false) Integer status,
                                                   @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date startTime,
                                                   @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date endTime) {
        Page<StandingBookReviewVO> page = standingBookReviewService.getPage(pageNum, pageSize, userInfoHolder.getAdminOrgId(),
                orgId, applicantName, contactPerson, status, startTime, endTime);
        return Result.success(page);
    }

    /**
     * 通过审核
     */
    @GetMapping("/pass")
    public Result pass(@RequestParam Long reviewId) {
        standingBookReviewService.approveReview(userInfoHolder.getAccountId(), reviewId);
        return Result.success();
    }

    /**
     * 不通过审核
     */
    @GetMapping("/deny")
    public Result deny(@RequestParam Long reviewId) {
        standingBookReviewService.denyReview(userInfoHolder.getAccountId(), reviewId);
        return Result.success();
    }

    /**
     * 驳回审核
     */
    @GetMapping("/reject")
    public Result reject(@RequestParam Long reviewId) {
        standingBookReviewService.rejectReview(userInfoHolder.getAccountId(), reviewId);
        return Result.success();
    }

    /**
     * 查看台账
     */
    @GetMapping("/info")
    public Result<StandingBookVO> info(@RequestParam Long reviewId) {
        StandingBookVO standingBookVO = standingBookService.getByReviewId(reviewId);
        return Result.success(standingBookVO);
    }

    /**
     * 查看台账资料
     */
    @GetMapping("/document")
    public Result<List<DocumentNumberVO>> getDocument(@RequestParam Long reviewId) {
        List<DocumentNumberVO> list = standingBookService.getDocumentByReviewId(reviewId);
        return Result.success(list);
    }

    /**
     * 保存台账信息
     */
    @PostMapping("/info/save")
    public Result saveStandingBook(@RequestBody @Valid StandingBookSaveInReviewDTO standingBookSaveInReviewDTO) {
        standingBookService.saveStandingBookByReview(standingBookSaveInReviewDTO);
        return Result.success();
    }

    /**
     * 保存台账资料
     */
    @PostMapping("/document/save")
    public Result saveDocument(@RequestBody @Valid StandingBookDocumentSaveDTO standingBookDocumentSaveDTO) {
        standingBookService.saveDocumentByReview(standingBookDocumentSaveDTO);
        return Result.success();
    }

    /**
     * 批量下载台账文档
     */
    @GetMapping("/document/download")
    public Result<String> download(@RequestParam Long reviewId) {
        String zipUrl = standingBookService.downloadDocumentByReviewId(reviewId);
        return Result.success(zipUrl);
    }

    /**
     * 查看台账日志
     */
    @GetMapping("/log")
    public Result<List<StandingBookReviewLogVO>> getLog(@RequestParam Long reviewId) {
        List<StandingBookReviewLogVO> list = standingBookReviewService.getLog(reviewId);
        return Result.success(list);
    }
}
